package com.api.mc
{
	import com.game.btnClass.*;
	
	import flash.display.*;
	import flash.text.*;
	
	public class ChangePage extends BasicMc
	{
		public var nextBtn:MovieClip;
		public var preBtn:MovieClip;
		public var txt:TextField;
		private var _changeFun:Function;
		private var _totalPage:uint = 0;
		private var _total:uint;
		private var _page:uint = 1;
		private var _perNum:uint;
		
		public function ChangePage()
		{
			return;
		}
		
		public function init(changeFun:Function, total:uint, perNum:uint, page:uint = 1) : void
		{
			if (total == 0)
			{
				return;
			}
			_perNum = perNum;
			_total = total;
			_totalPage = Math.ceil(_total / _perNum);
			_changeFun = changeFun;
			thisChangeFun();
			if (_totalPage <= 1)
			{
				return;
			}
			addMouseEvent(new BtnClass_goto(preBtn, thisPreFun));
			addMouseEvent(new BtnClass_goto(nextBtn, thisNextFun));
		}
		
		private function setTxt() : void
		{
			txt.text = _page + "/" + _totalPage;
		}
		
		private function thisPreFun() : void
		{
			if (_page > 1)
			{
				_page--;
			}
			else
			{
				return;
			}
			thisChangeFun();
		}
		
		private function thisNextFun() : void
		{
			if (_page < _totalPage)
			{
				_page++;
			}
			else
			{
				return;
			}
			thisChangeFun();
		}
		
		private function thisChangeFun() : void
		{
			var _sNum:uint = (_page-1)* _perNum + 1;
			var _eNum:uint = _page * _perNum;
			if (_eNum > _total)
			{
				_eNum = _total;
			}
			_changeFun(_sNum, _eNum);
			setTxt();
			setBtnVisible();
		}
		
		private function setBtnVisible() : void
		{
			if (_totalPage <= 1)
			{
				preBtn.visible = false;
				nextBtn.visible = false;
				return;
			}
			if (_page < _totalPage)
			{
				nextBtn.visible = true;
			}
			else
			{
				nextBtn.visible = false;
			}
			if (_page > 1)
			{
				preBtn.visible = true;
			}
			else
			{
				preBtn.visible = false;
			}
		}
		
	}
}
